package WebLinkService

import (
    "fmt"
    "server/app"
    "server/entity"
)

// 查询友情链接列表
func FindList() []entity.WebLinkType {
    db := app.GetDatabase()

    //读取友情链接类型
    typeList := make([]entity.WebLinkType, 0)
    typeSql := fmt.Sprintf("select Id,Name from LinkTypes where status=1 order by sort_id asc")
    err := db.Select(&typeList, typeSql)
    app.Panic(err)
    if len(typeList) == 0 {
        return typeList
    }

    //读取友情链接
    linkList := make([]entity.WebLink, 0)
    linkSql := fmt.Sprintf("select link_name,link_url,link_typeid from WebsiteLinks where status=1 order by sort_id asc")
    err1 := db.Select(&linkList, linkSql)
    app.Panic(err1)
    if len(linkList) == 0 {
        return typeList
    }

    for index, t := range typeList {
        typeList[index].Links = filterLinks(linkList, t.Id)
    }

    return typeList
}

//根据类型ID进行过滤
func filterLinks(list []entity.WebLink, typeId string) []entity.WebLink {
    result := make([]entity.WebLink, 0)
    for _, link := range list {
        if link.LinkTypeId == typeId {
            result = append(result, link)
        }
    }
    return result
}
